//
//  ZsTabBarController.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/4/22.
//

import UIKit

class ZsTabBarController: UITabBarController ,UITabBarControllerDelegate,UINavigationControllerDelegate{

    var selectedIndexC:Int! = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
//        self.tabBar.tintColor = UIColor.colorConvertFromString(rgbValue: 333333)
        self.tabBar.tintColor = UIColor.init(hexString: "#333333")
        let homeViewController = WLHomeController()
        self.addChild(self.createNavigationC(rootViewC: homeViewController, navigationBarTitle: ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_headPage) as NSString, tabBarTitle: ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_headPage) as NSString, normalImageName: "home_home_normal", selectedImageName: "home_home_select"))
        let orderViewController = OrderViewController()
        self.addChild(self.createNavigationC(rootViewC: orderViewController, navigationBarTitle: ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_order) as NSString, tabBarTitle: ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_order) as NSString, normalImageName: "home_order_normal", selectedImageName: "home_order_select"))
    }

    func createNavigationC(rootViewC:UIViewController,navigationBarTitle:NSString,tabBarTitle:NSString,normalImageName:NSString,selectedImageName:NSString) -> UINavigationController {
        let navigationC = ZsNavigationController.init(rootViewController: rootViewC);
        navigationC.delegate = self
        rootViewC.tabBarItem.title = tabBarTitle as String
        rootViewC.navigationItem.title = navigationBarTitle as String
        rootViewC.tabBarItem.image = UIImage.init(named: normalImageName as String)
        let selectedImage = UIImage.init(named: selectedImageName as String)?.withRenderingMode(.alwaysOriginal)
        rootViewC.tabBarItem.selectedImage = selectedImage;
        rootViewC.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.init(hexString: "#333333")], for: UIControl.State.selected)
        return navigationC
    }
    
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        let shouldIndex = tabBarController.children.firstIndex(of: viewController)
        if  shouldIndex == 0  && self.selectedIndexC != shouldIndex {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "TabBarClick"), object: nil)
        }
        self.selectedIndexC = shouldIndex
        return true
    }

}
